//http api
var api = new httpApi("http://114.119.4.79:8081");
//ocx api
var ocxService = new ocxService();
//ptt ocx api
var pttOcxService = new pttOcxService();

var loginUser;

//ocx call back function
var replaceStatus = function (status) {
    var rs = "";
    status = parseInt(status, 2);

    rs += ((status & 1) != 0) ? "" : "";
    rs += ((status & 2) != 0) ? "" : "";
    rs += ((status & 4) != 0) ? "" : "";
    rs += ((status & 8) != 0) ? "" : "";
    rs += ((status & 16) != 0) ? "视频, " : "音频, ";
    rs += ((status & 32) != 0) ? "录像, " : "未录像, ";
    rs += ((status & 64) != 0) ? "静音, " : "未静音, ";
    rs += ((status & 128) != 0) ? "闭音" : "未闭音";

    return rs;
}

var OnGetCallsInfo = function (nVar) {
    if (nVar == null || nVar == undefined || nVar == "")
        return;

    var rs = JSON.parse(nVar);
    rs.items.forEach(function (item) {
        var opStr = "pno: " + item.pno + ", uid: " + item.uid + ", status: ";
        opStr += replaceStatus(item.status);
        console.log(opStr);
    });
}

var OnCaptrueComplete = function (nVar) {
    console.log("截图路径: " + nVar)
}

var OnMp4RecordingCompleted = function (nVar) {
    console.log("视频路径: " + nVar);
}

//format json
function syntaxHighlight(json) {
    if (typeof json != 'string') {
        json = JSON.stringify(json, undefined, 2);
    }
    json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

/****************  basic http api  ************************/
function downloadCenter() {
    api.downloadCenter().then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function login() {
    api.login("gxsft", "123456").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function loadOcxInfo() {
    api.loadOcxInfo("sdktest").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function logout() {
    api.logout().then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryMenu() {
    api.queryMenu().then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryRole(id) {
    api.queryRole(id).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryRole() {
    api.fuzzyQueryRole("普通", null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryAuthorization() {
    api.queryAuthorization().then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveRoleAction() {
    var arr = [];
    arr.push({role_id: 4, id: 1, active: true});
    arr.push({role_id: 4, id: 2, active: true});
    arr.push({role_id: 4, id: 3, active: true});
    api.saveRoleAction(arr).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function changePassword() {
    api.changePassword("123", "1", "1").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQuerySystemUser() {
    api.fuzzyQuerySystemUser("sdktest", null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveSystemUser() {
    api.saveSystemUser(null, "abc", "123", "name", 2, 1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifySystemUser() {
    api.saveSystemUser("", "", "", "", 2, 1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeSystemUser() {
    api.removeSystemUser("").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryDepartment(id) {
    api.queryDepartment(id).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryDepartment() {
    api.fuzzyQueryDepartment(null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveDepartment() {
    api.saveDepartment(null, "", "", "", 1, null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifyDepartment() {
    api.saveDepartment(2, "", "", "", 1, "",
        new Date().getTime(), "").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeDepartment() {
    api.removeDepartment(9999).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryPerson(id) {
    api.queryPerson(113).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryPerson() {
    api.fuzzyQueryPerson("", null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function savePerson() {
    api.savePerson(null, "", "", 10, "").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifyPerson() {
    api.savePerson(101, "", "", 10, "").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removePerson() {
    api.removePerson(101, "", "").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryArea() {
    api.fuzzyQueryArea("t", null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveArea() {
    api.saveArea(null, "test", 1, "b60c326a-c7b6-47c4-8801-a709ca3fc13b", "1.0", "2.0", 5).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifyArea() {
    api.saveArea(2, "t11", 1, "b60c326a-c7b6-47c4-8801-a709ca3fc13b", "1.0", "2.0", 5).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeArea() {
    api.removeArea(2).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryFence(id) {
    api.queryFence(1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryFence() {
    api.fuzzyQueryFence("t", null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveFence() {
    api.saveFence(null, "test", 1, "b60c326a-c7b6-47c4-8801-a709ca3fc13b", "1.0", "2.0", "3.0", "4.0",
        null, 5, 0).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifyFence() {
    api.saveFence(2, "t11", 1, "b60c326a-c7b6-47c4-8801-a709ca3fc13b", "1.0", "2.0", "3.0", "4.0",
        "pno1,pno2,pn3", 5, 1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeFence() {
    api.removeFence(2).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryNotice() {
    api.queryNotice().then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveNotice() {
    api.saveNotice(null, "t1", null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifyNotice() {
    api.saveNotice(2, "demo", "http://www,google.com.hk").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeNotice() {
    api.removeNotice(2).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryLog() {
    api.fuzzyQueryLog(null, new Date("2016-08-20").getTime(), new Date("2016-08-28").getTime(), 1, 25).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function exportLog() {
    api.exportLog(null, new Date("2016-08-20").getTime(), new Date("2016-08-28").getTime());
}

function queryTrackInfo() {
    api.queryTrackInfo("test", "b60c326a-c7b6-47c4-8801-a709ca3fc13b", new Date("2016-08-20").getTime(), new Date("2016-08-28").getTime()).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryTerminal() {
    api.queryTerminal(23).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryTerminal() {
    api.fuzzyQueryTerminal().then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryWarning() {
    api.queryWarning(1, 25).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function checkWarning() {
    api.checkWarning([{id: 1}, {id: 2}]).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function delayDepartmentValidTime() {
    api.delayDepartmentValidTime(1, new Date().getTime()).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function loadTerminalConfig() {
    api.loadTerminalConfig(1, new Date().getTime()).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveTerminalConfig() {
    api.saveTerminalConfig(10, 60, 180, 180).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

/****************  basic http api  ************************/

/****************  advance http api  ************************/
function loadHisData() {
    api.loadHisData("0", null, null, null, new Date("2018-05-09 00:00:00").getTime(), new Date("2018-05-10 23:59:59").getTime(), 1, 25).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryDefaultChannel() {
    api.fuzzyQueryDefaultChannel("test", null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryTempChannel() {
    api.fuzzyQueryTempChannel(null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryChannelInfo() {
    api.queryChannelInfo(11100100000000000001, 0).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveTempChannel() {
    api.saveTempChannel(null, 1, 1, "eee", '000001').then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifyTempChannel() {
    api.saveTempChannel(1, "b60c326a-c7b6-47c4-8801-a709ca3fc13b", 1, 11100100000000000001, "t1", 1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeTempChannel() {
    api.removeTempChannel('11100100000000000002').then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function generateChannelRela() {
    api.generateChannelRela('11100100000000000002', [{sn: "xxx", pid: "xxx"}]).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeChannelRela() {
    api.removeChannelRela(11100100000000000001, [{sn: "x1", pid: "xxx"}, {sn: "x2", pid: "xxx"}]).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function dispatchPttInfo() {
    api.dispatchPttInfo('11100100000000000002', [{sn: "T6A15102610300111"}]).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryCtrlInfo() {
    api.queryCtrlInfo(1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function fuzzyQueryCtrlInfo() {
    api.fuzzyQueryCtrlInfo(null, null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function saveCtrlInfo() {
    var selectedFile = document.getElementById("ctrlInfoFileUpload").files[0];
    api.saveCtrlInfo(null, null, "test", "test", null, null, "0", null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function modifyCtrlInfo() {
    var selectedFile = document.getElementById("ctrlInfoFileUpload").files[0];

    api.saveCtrlInfo(selectedFile, null, "test", "test", null, null, null, null, null).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function removeCtrlInfo() {
    api.removeCtrlInfo(1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function queryCtrlTarget() {
    api.queryCtrlTarget(1).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function sendRemoteMessage() {
    api.sendRemoteMessage("5", "1", "tit", "content", "http://180.169.1.221:8081/upload/589e0626-ff0d-4279-b2e8-be53f0b5e048.png", "upload/589e0626-ff0d-4279-b2e8-be53f0b5e048.png", [
        {
            dkey: "xxx",
            pno: "xxx",
            sn: "xxx"
        }
    ]).then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function loadRom() {
    api.loadRom().then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function dispatchUpgrade() {
    api.dispatchUpgrade(1, "修复了一些bug", "8953358153ce8336b79314bdf7390255", 380355323, "xxxx", "http://xxxx/xxxx/ROM/T6/V003/xxxx.zip").then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function recordVideo() {
    api.recordVideo('T811610131050029', '1234546', 'start').then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

function remotePhoto() {
    api.remotePhoto('T811610131050029').then(function (data) {
        $("#output").html(syntaxHighlight(data));
    }, function (error) {
        $("#output").html(syntaxHighlight(error));
    });
}

/****************  advance http api  ************************/

/****************  ocx api  ************************/
function registeOcx() {
    api.login("gxsft", "123456").then(function (data) {
        return api.loadOcxInfo("gxsft");
    }, function (error) {

    }).then(function (data) {
        ocxService.registOcx(document.getElementById("Mymic"), data.uid, data.pname, data.server, data.ac);
    }, function (error) {

    });
}

function loadVideoPath() {
    $("#output").html(ocxService.loadConfig("video_path"));
}

function loadRecord_time() {
    $("#output").html(ocxService.loadConfig("split_record_time"));
}

function writeVideoPath() {
    ocxService.writeConfig("video_path", "d:/temp");
}

function writeRecord_time() {
    ocxService.writeConfig("split_record_time", "60");
}

var callData = null;

function callVideo(id) {
    api.queryTerminal(290).then(function (data) {
        if (data != null && data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].pno == id + "") {
                    callData = data[i];
                }
            }
            ocxService.callStream(1, callData);
        }
    }, function (error) {

    })
}

function callAudio() {
    if (callData == null) {
        api.queryTerminal(1).then(function (data) {
            if (data != null && data.length > 0) {
                callData = data[0];
                ocxService.callStream(2, callData);
            }
        }, function (error) {

        });
    } else
        ocxService.callStream(2, callData);
}

/****************  ocx api  ************************/

/****************  ptt ocx api  ************************/
function registePttOcx() {
    api.login("gxsft", "123456").then(function (data) {
        loginUser = data;
        return api.loadOcxInfo("gxsft");
    }, function (error) {

    }).then(function (data) {
        var pptSipPara = data.sipPara;
        pttOcxService.registOcx(document.getElementById("TonmxIntercomOCX"), pptSipPara.ip, pptSipPara.port, data.uid, data.ac, pptSipPara.priority, pptSipPara.flag, loginUser.roleid, loginUser.deptid, loginUser.dkey);
    }, function (error) {

    });
}

function startIntercome() {
    pttOcxService.startIntercome({gid: 11100100000000000001, gname: "test"});
}

function stopIntercome() {
    pttOcxService.stopIntercome();
}

function changeGName() {
    pttOcxService.changeGName("t1");
}

/****************  ptt ocx api  ************************/

/****************  jms api  ************************/
var registedCallback = function (data) {
    console.log(data);
}
var terminalInfoCallback = function (data) {
    console.log(data);
}
var warningInfoCallback = function (data) {
    console.log(data);
}

function subscribe() {
    api.login("test", "123").then(function (data) {
        registWebSocket("ws://127.0.0.1:61614", data.tp, registedCallback, terminalInfoCallback, warningInfoCallback);
    }, function (error) {

    });
}

/****************  jms api  ************************/
